﻿using CrawlerCore.Helper;
using CrawlerCore.XmlDataForeign;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace CrawlerCore.XmlDataTemp
{
    public class RootTemp
    {
        [XmlArray("Blocks")]
        [XmlArrayItem("Block")]
        public List<Block> blocks { get; set; }
        public string Departure { get; set; }

        public string Arrival { get; set; }

        public string AirLine = "";

        public RootForeign Convert2RootForeign(bool IsRoundTrip)
        {
            RootForeign r = new RootForeign();
            r.Departure = Departure;
            r.Arrival = Arrival;
            r.Flights = getListFlights(IsRoundTrip);
            return r;
        }

        private List<FlightForeign> getListFlights(bool IsRoundTrip)
        {
            List<FlightForeign> li = new List<FlightForeign>();
            try
            {
                bool isRoundTrip = true;
                if (blocks[0].inBound == null || blocks[0].inBound.Items == null || blocks[0].inBound.Items.Count == 0)
                    isRoundTrip = false;
                if (IsRoundTrip != isRoundTrip)
                    return li;
                int id = 0;
                if (IsRoundTrip)
                {
                    foreach (ItemTemp it in blocks[0].outBound.Items)
                    {
                        foreach (ItemTemp it1 in blocks[0].inBound.Items)
                        {
                            FlightForeign f = new FlightForeign();
                            f.Id = id;
                            id++;
                            f.Source = AirLineName.tiger;
                            //chieu di
                            OutInBoundForeign outBoundForeign = new OutInBoundForeign();
                            outBoundForeign.Segments = Convert2ListSegmentForeign(it.Segments);
                            f.outBoundForeign = outBoundForeign;
                            //chieu ve
                            OutInBoundForeign inBoundForeign = new OutInBoundForeign();
                            inBoundForeign.Segments = Convert2ListSegmentForeign(it1.Segments);
                            f.inBoundForeign = inBoundForeign;
                            li.Add(f);
                            f.PriceBaseOut = it.prices[0].price;
                            f.PriceBaseIn = it1.prices[0].price;
                        }
                    }
                }
                else
                {
                    foreach (ItemTemp it in blocks[0].outBound.Items)
                    {
                        FlightForeign f = new FlightForeign();
                        f.Id = id;
                        id++;
                        f.Source = AirLineName.tiger;
                        //chieu di
                        OutInBoundForeign outBoundForeign = new OutInBoundForeign();
                        outBoundForeign.Segments = Convert2ListSegmentForeign(it.Segments);
                        f.outBoundForeign = outBoundForeign;
                        f.PriceBaseOut = it.prices[0].price;
                        li.Add(f);
                    }
                }
            }
            catch { }
            return li;
        }

        private List<SegmentForeign> Convert2ListSegmentForeign(List<SegmentTemp> list)
        {
            List<SegmentForeign> li = new List<SegmentForeign>();
            foreach (SegmentTemp st in list)
            {
                SegmentForeign s = new SegmentForeign();
                s.AirLine = st.AirLine;
                s.Departure = st.Departure;
                s.Arrive = st.Arrive;
                s.FlightNo = st.flightNo;
                s.DepartureTime = st.departureTime;
                s.ArriveTime = st.arriveTime;
                s.LayOver = st.layOver;
                s.Class = st.Class;
                s.Duration = st.duration;
                li.Add(s);
            }
            return li;
        }
    }
}